package com.spider.dao;

import org.springframework.jdbc.core.JdbcTemplate;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.spider.bean.Article;

public class ArticleDao extends JdbcTemplate {

	private static int m = 0;

	public ArticleDao() {
		// 创建C3P0的datasource 1.配置 2.代码
		ComboPooledDataSource dataSource = new ComboPooledDataSource();
		// 1.url
		// 2.driver
		// 3.username&password
		dataSource.setUser("root");
		dataSource.setPassword("root");// 注意和老师电脑的名称一致 ,这是本地密码.本地数据库
		dataSource.setJdbcUrl("jdbc:mysql://192.168.22.134:3306/spider?characterEncoding=utf-8");
		setDataSource(dataSource);
	}

	public void save(Article article) {
		// 注意和老师电脑的名称一致
		String sql = "INSERT INTO spider.tanzhaoxiang007 (`id`, `title`, `author`, `createTime`, `content`, `url` ) VALUES( ?,?,?,?,?,?)";

		int i = update(sql, article.getId(), article.getArticleTitle(), article.getArticleAuthName(),
				article.getArticlePublishTime(), article.getArticleContent(), article.getArticleUrl());
		if (i >= 1) {
			m++;
			System.out.println("线程小弟:" + Thread.currentThread().getId() + "插入第:" + m + "条文章数据到数据库");
		}

	}
}
